﻿#region License
// (c) Intergen.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TextGlow.Control.Utilities;

namespace TextGlow.Control.Controls.Document
{
  public partial class PageControl : UserControlExtended
  {
    private double _marginBottom;
    private double _marginFooter;
    private double _marginGutter;
    private double _marginHeader;
    private double _marginLeft;
    private double _marginRight;
    private double _marginTop;

    public double MarginBottom
    {
      get { return _marginBottom; }
      set { _marginBottom = value; }
    }

    public double MarginFooter
    {
      get { return _marginFooter; }
      set { _marginFooter = value; }
    }

    public double MarginGutter
    {
      get { return _marginGutter; }
      set { _marginGutter = value; }
    }

    public double MarginHeader
    {
      get { return _marginHeader; }
      set { _marginHeader = value; }
    }

    public double MarginLeft
    {
      get { return _marginLeft; }
      set { _marginLeft = value; }
    }

    public double MarginRight
    {
      get { return _marginRight; }
      set { _marginRight = value; }
    }

    public double MarginTop
    {
      get { return _marginTop; }
      set { _marginTop = value; }
    }

    public UIElementCollection HeaderContent
    {
      get { return Header.Children; }
    }

    public UIElementCollection FooterContent
    {
      get { return Footer.Children; }
    }

    public UIElementCollection PageContentChildren
    {
      get { return PageContent.Children; }
    }

    public List<UIElement> RemainderChildren
    {
      get { return PageContent.RemainderChildren; }
    }

    public PageControl()
    {
      InitializeComponent();
      Loaded += new RoutedEventHandler(PageControl_Loaded);
    }

    private void PageControl_Loaded(object sender, RoutedEventArgs e)
    {
    }

    protected override Size MeasureOverride(Size availableSize)
    {
      double contentWidth = Width - _marginLeft - _marginRight;

      Header.CustomMeasure(new Size(contentWidth, double.PositiveInfinity));
      Footer.CustomMeasure(new Size(contentWidth, double.PositiveInfinity));

      double contentStart = Math.Max(_marginTop, Header.CustomDesiredSize.Height + _marginHeader);
      double contentEnd = Math.Max(_marginBottom, _marginFooter + Footer.CustomDesiredSize.Height);

      PageContent.Width = contentWidth;
      PageContent.CustomMeasure(new Size(contentWidth, availableSize.Height - contentStart - contentEnd));

      return availableSize;
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
      Rect backgroundBounds = LayoutUtils.CreateRect(LayoutUtils.Zero, finalSize);
      Background.SetBounds(backgroundBounds);

      double contentWidth = Width - _marginLeft - _marginRight;

      // place header at the top
      Point headerTopLeft = new Point(_marginLeft, _marginHeader);
      // desired header size
      Size headerSize = new Size(contentWidth, _marginTop - _marginHeader);

      //Header.SetBounds(LayoutUtils.CreateRect(headerTopLeft, headerSize));
      Header.CustomArrange(LayoutUtils.CreateRect(LayoutUtils.Zero, Header.CustomDesiredSize));
      Header.SetLocation(headerTopLeft);

      headerSize = Header.CustomDesiredSize;



      Point footerBottomLeft = new Point(_marginLeft, Height - _marginBottom);
      Size footerSize = new Size(contentWidth, double.PositiveInfinity);

      //Footer.VerticalAlignment = VerticalAlignment.Bottom;
      //Footer.SetBounds(LayoutUtils.CreateRect(footerTopLeft, footerSize));
      Footer.CustomArrange(LayoutUtils.CreateRect(LayoutUtils.Zero, footerSize));

      footerSize = Footer.CustomDesiredSize;
      Point footerTopLeft = new Point(_marginLeft, Height - _marginFooter - footerSize.Height);
      Footer.SetLocation(footerTopLeft);





      double contentStart = Math.Max(_marginTop, headerSize.Height + _marginHeader);
      double contentEnd = Math.Max(_marginBottom, _marginFooter + footerSize.Height);

      Point contentTopLeft = new Point(_marginLeft, Math.Max(_marginTop, contentStart));
      Size contentSize = new Size(contentWidth, Height - contentStart - contentEnd);

      //PageContent.SetBounds(LayoutUtils.CreateRect(contentTopLeft, contentSize));
      //PageContent.UpdateLayout();
      //PageContent.InvalidateArrange();
      PageContent.SetLocation(contentTopLeft);
      //PageContent.MinWidth = Width;
      PageContent.CustomArrange(LayoutUtils.CreateRect(LayoutUtils.Zero, contentSize));
      //PageContent.BeginUpdate();
      //PageContent.AutoSize = AutoSize.None;
      //PageContent.SetBounds(LayoutUtils.CreateRect(contentTopLeft, contentSize));
      //PageContent.Arrange(LayoutUtils.CreateRect(contentTopLeft, contentSize));
      //PageContent.EndUpdate();
      //PageContent.UpdateLayout();

      //Footer.UpdateLayout();


      return finalSize;
    }

    //protected override void PerformLayout()
    //{
    //  // place header at the top
    //  Point headerTopLeft = new Point(_marginLeft, _marginHeader);
    //  // desired header size
    //  Size headerSize = new Size(Width - _marginLeft - _marginRight, _marginTop - _marginHeader);

    //  Header.BeginUpdate();
    //  Header.AutoSize = AutoSize.Height;
    //  FrameworkElementBase.SetBounds(Header, LayoutUtils.CreateRect(headerTopLeft, headerSize));
    //  Header.EndUpdate();
    //  Header.UpdateLayout();

    //  // actual header size
    //  headerSize = FrameworkElementBase.GetSize(Header);


    //  // place footer at the bottom. bottom align footer content
    //  Point footerTopLeft = new Point(_marginLeft, Height - _marginBottom);
    //  Size footerSize = new Size(Width - _marginLeft - _marginRight, _marginBottom - _marginFooter);

    //  Footer.BeginUpdate();
    //  Footer.VerticalAlignment = VerticalAlignment.Bottom;
    //  Footer.AutoSize = AutoSize.None;
    //  FrameworkElementBase.SetBounds(Footer, LayoutUtils.CreateRect(footerTopLeft, footerSize));
    //  Footer.EndUpdate();
    //  Footer.UpdateLayout();

    //  double contentStart = Math.Max(_marginTop, headerSize.Height + _marginHeader);

    //  // place page content in the middle
    //  Point contentTopLeft = new Point(_marginLeft, Math.Max(_marginTop, contentStart));
    //  Size contentSize = new Size(Width - _marginLeft - _marginRight, Height - contentStart - _marginBottom);

    //  Content.BeginUpdate();
    //  Content.AutoSize = AutoSize.None;
    //  FrameworkElementBase.SetBounds(Content, LayoutUtils.CreateRect(contentTopLeft, contentSize));
    //  Content.EndUpdate();
    //  Content.UpdateLayout();
    //}
  }
}
